package org.kx.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.kx.bean.Student;
import org.kx.dao.StudentDao;
import org.kx.utils.MyBatisUtil;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectStudents() {
        //获取SqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        String sqlId = "org.kx.dao.StudentDao.selectStudents";
        //执行sql，使用SqlSession方法
        List<Student> studentList = sqlSession.selectList(sqlId);
        for (Student student : studentList) {
            System.out.println(student);
        }
        //关闭SqlSession
        sqlSession.close();
        return studentList;
    }

    @Override
    public int insertStudent(Student student) {
        //获取SqlSession对象
        SqlSession sqlSession=MyBatisUtil.getSqlSession();
        String sqlId="org.kx.dao.StudentDao.insertStudent";
        int numbers=sqlSession.insert(sqlId,student);
        sqlSession.commit();
        sqlSession.close();
        return numbers;
    }
}
